package cn.com.oceansoft.business.mobile.danger.util;

import cn.com.oceansoft.nos.cache.redis.CacheBaseService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 祝亚 on 2018-04-27.
 */
public class DictUtil {
    /***
     * 区-街道-社区，级联获取
     * @param parentid 父级id
     * @param deep 深度
     * @return
     */
    public static JSONArray getChildByParentid(String parentid, int deep) {
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        JSONArray result = new JSONArray();
        if(1==deep){
            list = (List<Map<String,Object>>) CacheBaseService.getInstance().getObject("sqxx_pcslist");
        }
        if(2==deep){
            list = (List<Map<String,Object>>) CacheBaseService.getInstance().getObject("sqxx_sqlist");
        }
        for(int i =0;i<list.size();i++){
            if(parentid.equals(list.get(i).get("PID"))){
                String s = JSONObject.toJSONString(list.get(i));
                result.add(JSONObject.parseObject(s));
            }
        }
        list=null;
        return result;
    }

    /***
     * 获取分局的JsonArray
     * @return
     */
    public static JSONArray getfjList() {
        List<Map<String,Object>> list = (List<Map<String,Object>>) CacheBaseService.getInstance().getObject("sqxx_fjlist");
        JSONArray result = new JSONArray();
        for(int i =0;i<list.size();i++){
            String s = JSONObject.toJSONString(list.get(i));
            result.add(JSONObject.parseObject(s));
        }
        list=null;
        return result;
    }

    /***
     * 根据ID获取所属信息
     * @return
     */
    public static JSONObject getObjectById(String id) {
        List<Map<String,Object>> list = (List<Map<String,Object>>) CacheBaseService.getInstance().getObject("sqxx_pcslist");
        JSONObject result = new JSONObject();
        for(int i =0;i<list.size();i++){
            if(id.equals(list.get(i).get("id"))){
                String s = JSONObject.toJSONString(list.get(i));
                result = JSONObject.parseObject(s);
                break;
            }
        }
        list=null;
        return result;
    }
    /***
     * 根据路名模糊匹配
     * @param jlxname
     * @return
     */
    public static JSONArray getJlxListByName(String jlxname) {
        List<Map<String,Object>> list = (List<Map<String,Object>>) CacheBaseService.getInstance().getObject("jlxlist");
       /* System.out.println("=========jlxlistJSONArray========");
        System.out.println("=========jlxlistJSONArray========");
        System.out.println(list);*/
        JSONArray result = new JSONArray();
        int count =0;
        for(int i =0;i<list.size();i++){
            //拼音首字母或者中文进行检索
            if(count>20){
                break;
            }
            if((null!=list.get(i).get("value")&&list.get(i).get("value").toString().indexOf(jlxname)>-1)||(null!=list.get(i).get("pyszm")&&list.get(i).get("pyszm").toString().indexOf(jlxname)>-1)){
                String s = JSONObject.toJSONString(list.get(i));
                result.add(JSONObject.parseObject(s));
                count++;
            }
            if("".equals(jlxname)||null==jlxname){
                if(i>=9){
                   break;
                }
            }
        }
        list=null;
        return result;
    }
}
